package sz.szsmk.citizencard.mvp.dzsmkpresenter;


import android.widget.Toast;

import com.google.gson.Gson;

import java.util.List;

import javax.inject.Inject;

import rx.functions.Action1;
import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.dzbean.req.ChangeBankCardRequest;
import sz.szsmk.citizencard.model.network.dzbean.req.CommonRequest;
import sz.szsmk.citizencard.model.network.dzbean.req.GetBankNameRequest;
import sz.szsmk.citizencard.model.network.dzbean.req.GetCheckCodeCcbReq;
import sz.szsmk.citizencard.model.network.dzbean.req.GetCheckCodeRequest;
import sz.szsmk.citizencard.model.network.dzbean.req.OldUserCheckRequest;
import sz.szsmk.citizencard.model.network.dzbean.req.RealNameAuthenticationRequest;
import sz.szsmk.citizencard.model.network.dzbean.res.AuthenticationResp;
import sz.szsmk.citizencard.model.network.dzbean.res.Bank;
import sz.szsmk.citizencard.model.network.dzbean.res.BaseRes;
import sz.szsmk.citizencard.model.network.dzbean.res.GetBankNameRes;
import sz.szsmk.citizencard.model.network.dzbean.res.GetJobRes;
import sz.szsmk.citizencard.model.network.dzbean.res.GetPeopleRes;
import sz.szsmk.citizencard.model.network.dzbean.res.OldCardRes;
import sz.szsmk.citizencard.model.network.dzbean.res.RealNameResponse;
import sz.szsmk.citizencard.mvp.RealNameAuthenticationMvpView;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.smkretrofit.NetworkDateSource;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.ObjectSignatureUtil;
import sz.szsmk.citizencard.widget.GlobalVariables;

/**
 * Created by yangzhengwen on 2017/10/2.
 */

public class RealNameAuthenticationPresenter extends AppBasePresenter<RealNameAuthenticationMvpView> {

    @Inject
    NetworkDateSource mNetworkDateSource;

    @Inject
    public RealNameAuthenticationPresenter() {
        getComponent().inject(this);
    }

    public void realNameAuthentication(String bankCode,
                                       final String realName,
                                       String idCardNumber,
                                       String bankCardNumber,
                                       String phoneInBank,
                                       String checkCode,
                                       String people,
                                       String address,
                                       String certStatDate,
                                       String certEndDate,
                                       String job

    ) {
        checkViewAttached();
        RealNameAuthenticationRequest realNameAuthenticationRequest = new RealNameAuthenticationRequest();
        String openId = ACache.get(DaggerApplication.getIns()).getAsString("openId");
        realNameAuthenticationRequest.setAddress(address);
        realNameAuthenticationRequest.setPeople(people);
        realNameAuthenticationRequest.setCertEndDate(certEndDate);
        realNameAuthenticationRequest.setCertStatDate(certStatDate);
        realNameAuthenticationRequest.setJob(job);
        realNameAuthenticationRequest.setOpenId(openId);
        realNameAuthenticationRequest.setRealName(realName);
        realNameAuthenticationRequest.setIdCardNumber(idCardNumber);
        realNameAuthenticationRequest.setBankCardNumber(bankCardNumber);
        realNameAuthenticationRequest.setPhoneInBank(phoneInBank);
        realNameAuthenticationRequest.setCheckCode(checkCode);
        realNameAuthenticationRequest.setBankCode(bankCode);
        realNameAuthenticationRequest.setIdCardType("00");

        String token = ACache.get(DaggerApplication.getIns()).getAsString("token");
        ObjectSignatureUtil.signatureZhsz(realNameAuthenticationRequest, token);
        String json = new Gson().toJson(realNameAuthenticationRequest);
        L.e(Constant.TAG, "realNameAuthenticationRequest---->" + json);
        mNetworkDateSource.realName(realNameAuthenticationRequest, new Action1<RealNameResponse>() {
            @Override
            public void call(RealNameResponse realNameResponse) {
                String code = realNameResponse.getCode();
                if (code.equals("0000")) {
                    RealNameResponse.DataBean data = realNameResponse.getData();
                    String json = new Gson().toJson(data);
                    AuthenticationResp authenticationResp = new Gson().fromJson(json, AuthenticationResp.class);
                    getMvpView().onAuthenticationSuccess(authenticationResp);
                } else {
                    String message = realNameResponse.getMessage();
                    getMvpView().onAuthenticationFail(message);
                }
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    public void oldUserCheck(int type) {
        checkViewAttached();
        OldUserCheckRequest request = new OldUserCheckRequest();
        String token = ACache.get(DaggerApplication.getIns()).getAsString("token");
        String openId = ACache.get(DaggerApplication.getIns()).getAsString("openId");
        request.setTradeType(type);
        request.setOpenId(openId);
        ObjectSignatureUtil.signatureRequest(request);
//        ObjectSignatureUtil.signatureZhsz(request, token);
        final String json = new Gson().toJson(request);
        L.e(Constant.MTAG, "---->oldUserCheck" + json);
        mNetworkDateSource.oldUserCheck(request, new Action1<OldCardRes>() {
            @Override
            public void call(OldCardRes baseRes) {
                String json1 = new Gson().toJson(baseRes);
                L.e(Constant.MTAG,"---->"+json1);
                if (baseRes.getCode().equals("0000")) {
                    getMvpView().onAuthenticationSuccess();
                } else {

                    getMvpView().onAuthenticationFail(baseRes.getMessage());
                    Toast.makeText(DaggerApplication.getIns(), baseRes.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }


    public void changeMyBankCard(String bankCode, String bankCardNumber, String phoneInBank, String checkCode) {
        checkViewAttached();
        ChangeBankCardRequest changeBankCardRequest = new ChangeBankCardRequest();
        changeBankCardRequest.setBankCardNumber(bankCardNumber);
        changeBankCardRequest.setPhoneInBank(phoneInBank);
        changeBankCardRequest.setCheckCode(checkCode);
        changeBankCardRequest.setBankCode(bankCode);
        ObjectSignatureUtil.signatureRequest(changeBankCardRequest);
        String json = new Gson().toJson(changeBankCardRequest);
        L.e(Constant.MTAG, "换卡请求报文：---->" + json);
        mNetworkDateSource.changeMyBankCard(changeBankCardRequest, new Action1<RealNameResponse>() {
            @Override
            public void call(RealNameResponse realNameResponse) {
                String code = realNameResponse.getCode();
                if (code.equals("0000")) {
                    RealNameResponse.DataBean data = realNameResponse.getData();
                    String json = new Gson().toJson(data);
                    AuthenticationResp authenticationResp = new Gson().fromJson(json, AuthenticationResp.class);
                    getMvpView().onAuthenticationSuccess();
                } else {
                    getMvpView().onAuthenticationFail(realNameResponse.getMessage());
                }
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });

    }

    public void getPeople() {
        // TODO: channelId根据用户选择的不同银行动态配置，这里以南京银行000001  建设银行000003 这里以南京银行为例
        CommonRequest commonRequest = new CommonRequest();
        mNetworkDateSource.getPeople(commonRequest, new Action1<GetPeopleRes>() {
            @Override
            public void call(GetPeopleRes ibaseBean) {
                if (ibaseBean.getCode().equals("0000")) {
                    if (ibaseBean.getData() != null) {

                        getMvpView().onGetPeopleSucc(ibaseBean.getData());
                    }
                } else {
                    getMvpView().onGetPeopleFail();
                }
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });


    }

    public void getJob() {
        CommonRequest commonRequest = new CommonRequest();
        mNetworkDateSource.getJob(commonRequest, new Action1<GetJobRes>() {
            @Override
            public void call(GetJobRes ibaseBean) {
                if (ibaseBean.getCode().equals("0000")) {
                    List<GetJobRes.DataBean> data = ibaseBean.getData();
                    getMvpView().onGetJobSucc(data);
                } else {
                    getMvpView().onGetJobFail();
                }
            }

        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    public void getCheckCodeCcbReq(String bankCardNumber, String idCardNumber, String phoneInBank, String realName) {
        GetCheckCodeCcbReq ccbReq = new GetCheckCodeCcbReq();

        ccbReq.setBankCardNumber(bankCardNumber);
        ccbReq.setIdCardNumber(idCardNumber);
        ccbReq.setPhoneInBank(phoneInBank);
        ccbReq.setOpenId(GlobalVariables.getInstance(DaggerApplication.getIns()).getPreAuthResult().getOpenId());
        ccbReq.setRealName(realName);
        ObjectSignatureUtil.signatureZhsz(ccbReq, "");
        final String json = new Gson().toJson(ccbReq);
        L.e(Constant.MTAG, "建设银行获取短信---->" + json);
        mNetworkDateSource.getCcbSmsCode(ccbReq, new Action1<BaseRes>() {
            @Override
            public void call(BaseRes baseRes) {
//                String json1 = new Gson().toJson(baseRes);
//                L.e(Constant.MTAG, "建设银行获取短信响应：" + json1);
//                getMvpView().onGetCheckCode();


                String code = baseRes.getCode();
                if (code.equals("0000")) {
                    getMvpView().onGetCheckCode();
                } else {
                    getMvpView().onGetCheckCodeFail(baseRes);
                }
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });


    }

    public void getCheckCode(String phoneNum, int checkCodeType) {
        checkViewAttached();
        GetCheckCodeRequest getCheckCodeRequest = new GetCheckCodeRequest();
        getCheckCodeRequest.setPhoneNumber(phoneNum);
        getCheckCodeRequest.setCheckCodeType(checkCodeType);
        ObjectSignatureUtil.signatureRequest(getCheckCodeRequest);
        String json = new Gson().toJson(getCheckCodeRequest);
        L.e(Constant.TAG, "getCheckCode------>" + json);
        mNetworkDateSource.getCheckCode(getCheckCodeRequest, new Action1<BaseRes>() {
            @Override
            public void call(BaseRes baseRes) {
                String code = baseRes.getCode();
                if (code.equals("0000")) {
                    getMvpView().onGetCheckCode();
                } else {
                    getMvpView().onGetCheckCodeFail(baseRes);
                }
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    public void getBankName(String bankCardNumber) {
        checkViewAttached();
        GetBankNameRequest getBankNameRequest = new GetBankNameRequest();
        getBankNameRequest.setBankCardNumber(bankCardNumber);
        ObjectSignatureUtil.signatureRequest(getBankNameRequest);
        mNetworkDateSource.getBankName(getBankNameRequest, new Action1<GetBankNameRes>() {
            @Override
            public void call(GetBankNameRes getBankNameRes) {
                String code = getBankNameRes.getCode();
                if (code.equals("0000")) {
                    GetBankNameRes.DataBean data = getBankNameRes.getData();
                    if (data == null) {
                        return;
                    }
                    String json = new Gson().toJson(data);
                    Bank bank = new Gson().fromJson(json, Bank.class);
                    getMvpView().onGetBankName(bank);
                } else {
                    getMvpView().onGetBankNamefail();
                }
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                getMvpView().onGetBankNamefail();
            }
        });

    }
}
